import { createRouter, createWebHistory } from 'vue-router'
import {useUserStore} from "@/stores/userStore.js";



const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      component: () => import('@/views/Login/index.vue')
    },
    {
      path: '/',
      component: () => import('@/views/Layout/index.vue'),
      redirect: '/home',
      children: [
        {
          path: 'home',
          component: () => import('@/views/Home/index.vue')
        },
        {
          path: 'detail',
          component: () => import('@/views/Detail/index.vue')
        },
        {
          path: 'publish',
          component: () => import('@/views/Publish/index.vue')

        },
        {
          path: 'my',
          component: () => import('@/views/My/index.vue')
        },
        {
          path: 'navigation',
          component: () => import('@/views/Navigation/index.vue')
        },
        {
          path: 'model',
          component: () => import('@/views/Model/index.vue')
        }
      ]
    },
    {
      path: '/:pathMatch(.*)*',
      component: () => import('@/views/NotFound/index.vue')
    }
  ],
})

router.beforeEach((to, from, next) => {
  const userStore = useUserStore() // 获取用户仓库
  const token = userStore.token // 获取 token

  if (token) {
    next()
  } else {
    if (to.path === '/login') {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
